package com.Maykk.Demo02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @ClassName: ReceiveUDP
 * @Description: TODO UDP 接收
 * @Author: MaykkStar@163.com
 */
public class ReceiveUDP {
    public static void main(String[] args) throws IOException {
        // 实例化接受对象
        DatagramSocket ds = new DatagramSocket(9527);
        // 接收数据
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp);
        // 输出数据
        byte[] data = dp.getData();
        int length = dp.getLength();
        System.out.println(new String(data, 0, length));
        // 关闭
        ds.close();
    }
}
